﻿/******************************************************************
** 文件名: SortPags.cs
** Copyright (c) 2005-2007南京奥派（AllPass）公司研发部
** 创建人: 吴晶
** 创建日期: 2007-11-02
** 修改人:
** 修改日期:
** 描 述: 排序分页类(传入DataTable,及相关信息,然后分页,并排序)
** 版 本: 1.0
**-功能模块:
 *      
******************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Sale_Operation
{
    /// <summary>
    /// SortPags 的摘要说明
    /// </summary>
    public class SortPags
    {
        /// <summary>
        /// 分页排序类
        /// </summary>
        /// <param name="dt">要分页或排序的数据源</param>
        public SortPags(DataTable dt)
        {
            this._DtSource = dt;
        }
        public SortPags(DataView dv)
        {
            this._DvSource = dv;
        }
        /// <summary>
        /// 存储传入的数据
        /// </summary>
        private DataTable _DtSource = null;
        private DataView _DvSource = null;
        /// <summary>
        /// 页面总数
        /// </summary>
        private int _PageCount;
        /// <summary>
        /// 每页记录数量
        /// </summary>
        private int _PageSize;
        /// <summary>
        /// 记录总数数
        /// </summary>
        private int _RowCount;
        /// <summary>
        /// 排序类型 SortType.ASC 升序 SortType.DESC 降序
        /// </summary>
        private SortType _SortKind;
        private int _CurrentPageIndex;
        /// <summary>
        /// 当前页面index
        /// </summary>
        public int CurrentPageIndex
        {
            get { return _CurrentPageIndex; }
        }
        /// <summary>
        /// 数据源
        /// </summary>
        public DataTable DtSource
        {
            get { return this._DtSource; }
        }
        /// <summary>
        /// 页面数量
        /// </summary>
        public int PageCount
        {
            get { return _PageCount; }
        }
        public int PageSize
        {
            get { return _PageSize; }
            set { _PageSize = value; }
        }
        /// <summary>
        /// 只读、不能写，获取该数据源那数据总数
        /// </summary>
        public int RowCount
        {
            get { return this._RowCount; }
        }
        public SortType SortKind
        {
            get { return _SortKind; }
            set { _SortKind = value; }
        }

        public DataView Sort(string sortName, SortType sortKind)
        {
            return new DataView();
        }
        /// <summary>
        /// 获取按照给定字段分页后的制定页，（排序－>分页）
        /// </summary>
        /// <param name="sortName">传入排序的字段</param>
        /// <param name="sortKind">排序的类型:SortType.ASC 升序 SortType.DESC 降序</param>
        /// <param name="pageSize">页面的大小（页面内要显示的记录的数量）</param>
        /// <param name="currentPageIndex">当前页面的index</param>
        /// <returns></returns>
        public DataTable GetCurrentPageSortByFieldName(string sortName, SortType sortKind, int pageSize, int currentPageIndex)
        {
            if (pageSize == 0) return DtSource; //如果没有填写pagesize那么返回整个数据源
            if (currentPageIndex <= 0) return DtSource; //如果没有传入当前页面index,则返回整个数据源
            if (sortName == "") return GetCurrentPage(pageSize, currentPageIndex);//如果排序字段没写，则只有分页，不进行排序

            DataView dv = new DataView(DtSource);
            switch (sortKind)
            {
                case SortType.DESC:
                    dv.Sort = sortName + " DESC";
                    break;
                case SortType.ASC:
                    dv.Sort = sortName + " ASC";
                    break;
                default: break;
            }

            _PageSize = pageSize;
            _CurrentPageIndex = currentPageIndex;
            this._RowCount = this.DtSource.Rows.Count;
            this._PageCount = this.RowCount / this.PageSize;
            if (_PageCount * PageSize < RowCount) //如果计算出的页面数*页面上的数据量小于记录数，那么页面大小自动+1
                _PageCount++;
            int CurrentBeginRowIndex = PageSize * (currentPageIndex - 1); //当前页面的开始行
            int CurrentEndRowIndex = PageSize * currentPageIndex - 1; //当前页面的结束行 
            DataTable dtRes = _DtSource.Clone(); //复制数据源表结构 
            for (int i = CurrentBeginRowIndex; i <= CurrentEndRowIndex; i++) //复制当前页面的数据到新的datatable中
            {
                if (i >= DtSource.Rows.Count) break;    //当前页面的记录小于该页面应该显示的记录时，就只取当前页面中的数据
                DataRow dr = dtRes.NewRow();
                for (int j = 0; j < _DtSource.Columns.Count; j++)
                {
                    dr[j] = dv[i][j];
                }
                dtRes.Rows.Add(dr);
            }
            return dtRes;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageSize">每页面大小（每个页面上记录的数量）</param>
        /// <param name="currentPage">当前页面索引</param>
        /// <returns></returns>
        public DataTable GetCurrentPage(int pageSize, int currentPageIndex)
        {
            if (pageSize == 0) return DtSource; //如果没有填写pagesize那么返回整个数据源
            if (currentPageIndex <= 0) return DtSource; //如果没有传入当前页面index,则返回整个数据源
            _PageSize = pageSize;
            _CurrentPageIndex = currentPageIndex;
            this._RowCount = this.DtSource.Rows.Count;
            this._PageCount = this.RowCount / this.PageSize;
            if (_PageCount * PageSize < RowCount) //如果计算出的页面数*页面上的数据量小于记录数，那么页面大小自动+1
                _PageCount++;
            int CurrentBeginRowIndex = PageSize * (currentPageIndex - 1); //当前页面的开始行
            int CurrentEndRowIndex = PageSize * currentPageIndex - 1; //当前页面的结束行 
            DataView dv;
            if (_DvSource == null)
                dv = new DataView(DtSource);
            else
                dv = _DvSource;
            DataTable dtRes = _DtSource.Clone(); //复制数据源表结构
            for (int i = CurrentBeginRowIndex; i <= CurrentEndRowIndex; i++) //复制当前页面的数据到新的datatable中
            {
                if (i >= DtSource.Rows.Count) break;    //当前页面的记录小于该页面应该显示的记录时，就只取当前页面中的数据
                DataRow dr = dtRes.NewRow();
                for (int j = 0; j < _DtSource.Columns.Count; j++)
                {
                    dr[j] = dv[i][j];
                }
                dtRes.Rows.Add(dr);
            }
            return dtRes;
        }
        public enum SortType
        {
            ASC, //升序排列 
            DESC  //倒序排列
        }
    }
}